/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.storage;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;

import com.jweb.framework.core.Context;
import com.jweb.framework.core.util.ApplicationContextHelper;
import com.jweb.framework.security.data.safe.DataFieldDecrypt;
import com.jweb.framework.security.data.safe.DataFieldEncrypt;
import com.jweb.framework.security.data.safe.IDecrypt;
import com.jweb.framework.security.data.safe.IEncrypt;
import com.jweb.framework.security.data.transform.DataPwdPassFac;
import com.jweb.framework.security.pass.IDecryptPass;
import com.jweb.framework.core.Action;

import com.enfry.storage.Response;
import com.enfry.storage.common.Constants;
import com.enfry.storage.exception.ExCode;
import com.enfry.storage.exception.StorageException;

public class BaseService implements Action, InitializingBean {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    protected String passwd;
    protected String securityId = "DATA001";
    protected IDecrypt decrypt;
    protected IEncrypt encrypt;

    /**
     * ajax输出文件字节数据
     *
     * @param ctx
     * @param bytes
     */
    public void ajaxBytes(Context ctx, byte[] bytes, boolean isEncrypted) {
        ctx.setData(isEncrypted ? Constants.AJAX_DECRYPT_OUTPUT_NAME : Constants.AJAX_OUTPUT_NAME, bytes);
    }
    
    private void ajaxResponse(Context ctx, String code, String msg, Map<String, Object> data) {
        Response response = new Response(code, msg, data);
        try {
            ctx.setData(Constants.AJAX_OUTPUT_NAME, response.toString().getBytes(Constants.CHARSET_UTF8));
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
            ctx.setData(Constants.AJAX_OUTPUT_NAME, response.toString().getBytes());
        }
    }
    
    public void ajaxData(Context ctx, Map<String, Object> data) {
        ajaxResponse(ctx, "0", null, data);
    }
    
    public void ajaxError(Context ctx, ExCode code) {
        ajaxResponse(ctx, code.getCode(), code.getMsg(), null);
    }
    
    public void ajaxError(Context ctx, ExCode code, Map<String, Object> data) {
        ajaxResponse(ctx, code.getCode(), code.getMsg(), data);
    }
    
    public void ajaxError(Context ctx, StorageException e) {
        ajaxResponse(ctx, e.getCode(), e.getDefaultMessage(), null);
    }
    
    public void ajaxSuccess(Context ctx) {
        ajaxResponse(ctx, "0", null, null);
    }
    
    public void ajaxSuccess(Context ctx, String msg) {
        ajaxResponse(ctx, "0", msg, null);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        JdbcTemplate jdbcTemplate = ApplicationContextHelper.getBean("jdbcTemplate");
        IDecryptPass idecryptpass = DataPwdPassFac.getDecryptPass();
        this.passwd = idecryptpass.getDePass(jdbcTemplate , securityId);

        this.decrypt = new DataFieldDecrypt();
        this.encrypt = new DataFieldEncrypt();
    }
}
